package com.schematic.flixel
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TouchEvent;
import flash.events.TransformGestureEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.ui.Multitouch;

import org.flixel.FlxPoint;

/**
 * Provides an invisible game controller
 */
public class Controller extends Sprite
{
    //--------------------------------------------------------------------------
    //
    //  Class Constants
    //
    //--------------------------------------------------------------------------
    
    //----------------------------------
    //  COLOR
    //----------------------------------
    /**
     * The default debug color
     */
    public static const COLOR:int = 0xFF0000;
    
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new instance of this class.
     */
    public function Controller(debug:Boolean = false)
    {
        super();
        this.debug = debug;

        pad = new Hotspot(24, 679, 110, 110, debug);
        aButton = new Hotspot(287, 708, 74, 74, debug);
        bButton = new Hotspot(384, 708, 74, 74, debug);
        
        pad.addEventListener(TouchEvent.TOUCH_BEGIN, pad_touchBeginHandler);
        pad.addEventListener(TouchEvent.TOUCH_MOVE, pad_touchMoveHandler);
        pad.addEventListener(TouchEvent.TOUCH_END, pad_touchEndHandler);
        pad.addEventListener(TouchEvent.TOUCH_OUT, pad_touchOutHandler);
        
        aButton.addEventListener(TouchEvent.TOUCH_BEGIN, aButton_touchBeginHandler);
        aButton.addEventListener(TouchEvent.TOUCH_END, aButton_touchEndHandler);
        aButton.addEventListener(TouchEvent.TOUCH_OUT, aButton_touchOutHandler);
        
        bButton.addEventListener(TouchEvent.TOUCH_BEGIN, bButton_touchBeginHandler);
        bButton.addEventListener(TouchEvent.TOUCH_END, bButton_touchEndHandler);
        bButton.addEventListener(TouchEvent.TOUCH_OUT, bButton_touchOutHandler);
        
        addChild(pad);
        addChild(aButton);
        addChild(bButton);
        
        addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  aButton
    //----------------------------------
    /**
     *  @private
     */
    private var aButton:Hotspot;

    //----------------------------------
    //  bButton
    //----------------------------------
    /**
     *  @private
     */
    private var bButton:Hotspot;

    //----------------------------------
    //  pad
    //----------------------------------
    /**
     *  @private
     */
    private var pad:Hotspot;

    //----------------------------------
    //  debug
    //----------------------------------
    /**
     *  @private
     */
    private var debug:Boolean;

    //----------------------------------
    //  monitor
    //----------------------------------
    /**
     *  @private
     */
    private var monitor:TextField;

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  up
    //----------------------------------

    /**
     *  @private
     */
    private var _up:Boolean;

    /**
     * true if joystick is pointing up
     */
    public function get up():Boolean
    {
        return _up;
    }

    //----------------------------------
    //  down
    //----------------------------------

    /**
     *  @private
     */
    private var _down:Boolean;

    /**
     * true if joystick is pointing down
     */
    public function get down():Boolean
    {
        return _down;
    }

    //----------------------------------
    //  left
    //----------------------------------

    /**
     *  @private
     */
    private var _left:Boolean;

    /**
     * true if joystick is pointing left
     */
    public function get left():Boolean
    {
        return _left;
    }

    //----------------------------------
    //  right
    //----------------------------------

    /**
     *  @private
     */
    private var _right:Boolean;

    /**
     * true if joystick is pointing right
     */
    public function get right():Boolean
    {
        return _right;
    }

    //----------------------------------
    //  aButtonDown
    //----------------------------------

    /**
     *  @private
     */
    private var _aButtonDown:Boolean;

    /**
     * True if A button is down
     */
    public function get aButtonDown():Boolean
    {
        return _aButtonDown;
    }

    //----------------------------------
    //  bButtonDown
    //----------------------------------

    /**
     *  @private
     */
    private var _bButtonDown:Boolean;

    /**
     * True if B button is down
     */
    public function get bButtonDown():Boolean
    {
        return _bButtonDown;
    }
    
    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  drawController
    //----------------------------------
    /**
     * @private
     */
    private function drawController(controller:Sprite, width:Number, height:Number,
                                                        color:int=0x00FF00):void
    {
        var controlAlpha:Number = debug ? 1 : 0;
        controller.graphics.clear();
        controller.graphics.beginFill(color, controlAlpha);
        controller.graphics.drawRect(0, 0, width / 2, height / 2);
        controller.graphics.endFill();
    }

    //----------------------------------
    //  createHotspots
    //----------------------------------
    /**
     *  @private
     */
    private function createHotspots():void
    {
    } 

    //----------------------------------
    //  createMonitor
    //----------------------------------
    /**
     * @private
     */
    private function createMonitor():void
    {
        monitor = new TextField();
        monitor.width = stage.stageWidth;
        monitor.height = stage.stageHeight / 2;
        var textFormat:TextFormat = new TextFormat("Arial", "18", 0xFFFFFF);
        textFormat.align = TextFormatAlign.CENTER;
        monitor.defaultTextFormat = textFormat;
        monitor.text = "- DEBUG MODE -\n";
        monitor.appendText("GREEN: Joystic (swipe)\n");
        monitor.appendText("RED:Button (click)\n");
        monitor.appendText("Multitouch.inputMode: " +  Multitouch.inputMode + "\n");
        addChild(monitor);
    } 

    //----------------------------------
    //  updatePadDirection
    //----------------------------------
    /**
     * @private
     */
    private function updatePadDirection(point:FlxPoint):void
    {
    
    } 
    
    //----------------------------------
    //  updateMonitor
    //----------------------------------
    /**
     * @private
     */
    private function updateMonitor(text:String):void
    {
        monitor.text = text +  "\n" + monitor.text;
    } 

    //--------------------------------------------------------------------------
    //
    //  Event Handlers
    //
    //--------------------------------------------------------------------------
    
    /**
     *  @private
     */    
    private function addedToStageHandler(event:Event):void
    {
        createHotspots();
        
        if (debug)
            createMonitor();
    }
    
    //----------------------------------
    //  pad_touchBeginHandler
    //----------------------------------
    /**
     * @private
     */
    private function pad_touchBeginHandler(event:TouchEvent):void
    {
        updatePadDirection(new FlxPoint(event.localX, event.localY));
    }

    //----------------------------------
    //  pad_touchMoveHandler
    //----------------------------------
    /**
     * @private
     */
    private function pad_touchMoveHandler(event:TouchEvent):void
    {
        updatePadDirection(new FlxPoint(event.localX, event.localY));
     }
    
    //----------------------------------
    //  pad_touchEndHandler
    //----------------------------------
    /**
     * @private
     */
    private function pad_touchEndHandler(event:TouchEvent):void
    {
        _left = _right = _up = _down = false;
    }

    //----------------------------------
    //  pad_touchOutHandler
    //----------------------------------
    /**
     * @private
     */
    private function pad_touchOutHandler(event:TouchEvent):void
    {
    
    }

    //----------------------------------
    //  aButton_touchBeginHandler
    //----------------------------------
    /**
     * @private
     */
    private function aButton_touchBeginHandler(event:TouchEvent):void
    {
        _bButtonDown = true;
        if (debug)
            updateMonitor("bButtonDown: " + bButtonDown); 
    }

    //----------------------------------
    //  aButton_touchOutHandler
    //----------------------------------
    /**
     * @private
     */
    private function aButton_touchOutHandler(event:TouchEvent):void
    {
    
    }   
    
    //----------------------------------
    //  aButton_touchEndHandler
    //----------------------------------
    /**
     * @private
     */
    private function aButton_touchEndHandler(event:TouchEvent):void
    {
    
    }

    //----------------------------------
    //  bButton_touchBeginHandler
    //----------------------------------
    /**
     * @private
     */
    private function bButton_touchBeginHandler(event:TouchEvent):void
    {
    
    }

    //----------------------------------
    //  bButton_touchOutHandler
    //----------------------------------
    /**
     * @private
     */
    private function bButton_touchOutHandler(event:TouchEvent):void
    {
    
    }

    //----------------------------------
    //  bButton_touchEndHandler
    //----------------------------------
    /**
     * @private
     */
    private function bButton_touchEndHandler(event:TouchEvent):void
    {
        _bButtonDown = false;
        if (debug)
            updateMonitor("bButtonDown: " + bButtonDown); 
    } 
    
}
}
import flash.display.Sprite;

class Hotspot extends Sprite
{
    public function Hotspot(x:Number, y:Number, width:Number, height:Number, debug:Boolean=false)
    {
        graphics.beginFill(0x00FF00, debug ? 0.5 : 0);
        graphics.drawRect(x, y, width, height);
        graphics.endFill();
    }
}